package com.parking.user.rabbit;

import cn.hutool.core.util.ObjectUtil;
import com.parking.common.core.model.RedisKey;
import com.parking.common.core.model.StateData;
import com.parking.common.web.util.RedisUtil;
import com.parking.common.web.util.SmsUtil;
import com.parking.exception.UserException;
import com.parking.exception.UserExceptionCode;
import com.parking.user.dao.UserAccountMapper;
import com.parking.user.dao.UserLoginLogsMapper;
import com.parking.user.entity.UserAccount;
import com.parking.user.entity.UserLoginLogs;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Map;

@Component
public class UserConsumer {
    //和队列相对应的处理消费者队列的
    @Resource
    private UserLoginLogsMapper userLoginLogsMapper;
    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private SmsUtil smsUtil;


    /**
     *用户登录日志
     * map参数：
     * userLoginLogs：用户登录日志对象
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "userLoginLogs")})
    private void userLoginLogs(Map<String,Object> map){
        UserLoginLogs logs = (UserLoginLogs) map.get("userLoginLogs");
        logs.setUserLoginState(StateData.USER_LOGS_ISNOT_LOGIN);
        userLoginLogsMapper.insert(logs);
    }
    /**
     *用户注册日志
     * map参数：
     * userAddTime：用户注册时间
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "addUserAccount")})
    private void addUserAccount(Map<String,Object> map){
        UserAccount userAccount = (UserAccount)map.get("userAccount");
        userAccountMapper.updateById(userAccount);

    }
    /***
     * 用户注册验证码
     * map参数：
     ("用户电话")：phone;
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "sendRegisterCode")})
    private void sendRegisterCode(Map<String,Object> map) throws Exception {
        String phone = (String) map.get("phone");
        //获取一小时内短信数量
        String number = redisUtil.get(RedisKey.userCodeOnHour(phone));
        if(number!=null&&Integer.parseInt(number)==5){
            throw new UserException(UserExceptionCode.USERINFO_PHONE_HOUR);
        }
        //发送验证码
//        String code="789789";
        String code = smsUtil.send(phone);

        //将验证码储存进redis
        redisUtil.set(RedisKey.userRegisterCode(phone),code,60*5L);
        //验证码存储一分钟内是否发送验证码
        redisUtil.set(RedisKey.userRegisterCodeOnMinute(phone),code,60L);
        System.out.println("测试参数5"+redisUtil.get(RedisKey.userRegisterCode(phone)));
        System.out.println("测试参数5"+redisUtil.get(RedisKey.userRegisterCodeOnMinute(phone)));
        //当前用户的手机号验证码一小时内短信是否存在
        if (redisUtil.exist(RedisKey.sendNumbers(phone))) {
            redisUtil.incr(RedisKey.sendNumbers(phone));
        }else {
            redisUtil.set(RedisKey.sendNumbers(phone), String.valueOf(1),60*60L);
        }


    }
    /***
     *
     * 用户登录日志
     * map参数：
     * ("用户电话")：phone;
     *  ("用户信息id")：userInfoId
     *   ("用户账号")：userAccount
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "codeLogin")})
    private void codeLogin(Map<String,Object> map) throws Exception{

        Integer userAccountId = (Integer) map.get("userAccountId");
        String userAccount = (String) map.get("userAccount");
        LocalDateTime userLoginTime = (LocalDateTime)map.get("userLoginTime");
        UserLoginLogs logs = new UserLoginLogs();
        logs.setUserAccountId(userAccountId);
        logs.setUserLoginTime(userLoginTime);
        logs.setUserLoginAccount(userAccount);
        logs.setUserLoginState(StateData.USER_LOGS_ISNOT_LOGIN);
        userLoginLogsMapper.insert(logs);


    }
    /***
     * 用户账号修改记录
     * map参数：
     * userUpdateTime：修改时间;
     * userUpdatePerson：修改人
     */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "upStateById")})
    private void upStateById(Map<String,Object> map){
        UserAccount userAccount = (UserAccount) map.get("userAccount");
        userAccountMapper.updateById(userAccount);

    }
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(name = "sendLoginCode")})
    private void sendLoginCode(Map<String,Object> map) throws Exception{
        String phone = (String) map.get("phone");
        //判断一小时内发送的验证码
        String number = redisUtil.get(RedisKey.userCodeOnHour(phone));
        if (number!=null&&Integer.parseInt(number)==5) {
            throw new UserException(UserExceptionCode.USERINFO_PHONE_HOUR);
        }
        //发送验证码
        String code = smsUtil.send(phone);
//        String code="789789";
        //验证码存储登录信息
        redisUtil.set(RedisKey.userLoginCode(phone),code,5*60L);
        //验证码存储一分钟内是否发送验证码
        redisUtil.set(RedisKey.userLoginCodeOnMinute(phone),code,60L);
        if (redisUtil.exist(RedisKey.sendNumbers(phone))) {
            redisUtil.incr(RedisKey.sendNumbers(phone));
        }else {
            redisUtil.set(RedisKey.sendNumbers(phone), String.valueOf(1),60*60L);
        }
    }

}
